package com.vcs.frm.service.client;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcs.frm.dao.client.ClientDao;
import com.vcs.frm.dao.common.CommonDao;
import com.vcs.frm.domain.Client;
import com.vcs.frm.domain.Job;
import com.vcs.frm.domain.User;
import com.vcs.frm.domain.UserRole;
import com.vcs.frm.utils.Constants;
import com.vcs.frm.web.form.client.NewJobFormBean;

@Service
public class ClientServiceImpl implements ClientService {

	/* Logger object */
	private final Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private ClientDao clientDao;

	@Autowired
	private CommonDao commonDao;

	/* Getters and Setters */

	public void setClientDao(ClientDao clientDao) {
		this.clientDao = clientDao;
	}

	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}

	/* Business methods */

	@Override
	@Transactional
	public void registerClient(Client client, User user) {

		client.setCreatedDate(new Date());
		clientDao.registerClient(client, user);
	}

	@Override
	@Transactional
	public Client getClientProfileById(Long clientId) {

		return clientDao.getClientProfileById(clientId);
	}

	@Override
	@Transactional
	public void postNewJob(NewJobFormBean newJobFormBean,
			HttpServletRequest request) {

		/* get logged in client from session */
		Client c = (Client) request.getSession().getAttribute("client");

		Job job = new Job();
		job.setJobTitle(newJobFormBean.getJobTitle());
		job.setJobDescription(newJobFormBean.getJobDescription());
		job.setKeySkills(newJobFormBean.getKeySkills());
		job.setCreatedDate(new Date());

		Client client = clientDao.getClientProfileById(c.getId());
		// add client to job
		client.getCompany();
		job.setClient(client);
		// finally log job object being persisted.
		log.info("Job : " + job.getJobTitle() + " - posted by Client: "
				+ job.getClient().getClientName() + " - "
				+ job.getClient());
		
		clientDao.postNewJob(job);
	}

	@Override
	@Transactional
	public Client getClientProfileByUsername(String username) {

		log.info("inside getClientProfileByUsername - Service layer");
		/* get user by username */
		User user = commonDao.getUserByUsername(username);
		log.info("User from Db : " + user);

		/*
		 * Check if logged-in user is a client or a jobseeker, accordingly load
		 * his profile
		 */
		log.info("UserId : " + user.getUserId() + " ROLES : "
				+ user.getUserRoles());
		Client client = null;
		for (UserRole r : user.getUserRoles()) {

			if (r.getAuthority().equalsIgnoreCase(Constants.ROLE_CLIENT)) {

				/*
				 * Logged-in user is a client so load his profile from client
				 * table
				 */
				log.info("Logged-in user is Client.............");

				client = clientDao.getClientProfileById(user.getUserId());

			}

		}
		log.info("leaving getClientProfileByUsername - Service layer");
		return client;
	}

	@Override
	public List<Job> getJobsPostedByClient(Long clientId) {
		List<Job> jobs = clientDao.getJobsPostedByClient(clientId);
		return jobs;
	}

}
